//
//  XDMiyoshiBankTodayViewController.m
//  XDEducation
//
//  Created by 星道 on 17/5/13.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDMiyoshiBankTodayViewController.h"
#import "HNTodayModel.h"
#import "HNTodayCell.h"

@interface XDMiyoshiBankTodayViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    IBOutlet UILabel *_FlowerLabel;         //红花数
    IBOutlet UILabel *_StarLabel;           //星星数
    IBOutlet UILabel *_HeartLabel;          //红心数
    IBOutlet UILabel *_CountProblemLabel;   //做题数
    IBOutlet UILabel *_ErrProblemLabel;     //错题数
    IBOutlet UITableView *_RightTableView;  //表单数据
    
    __weak UIButton *_TempButton;   //临时保存左边按钮空间
    IBOutletCollection(UIButton) NSArray *_Buttons;
}
@property (nonatomic, strong) HNTodayModel *Model;
@end

@implementation XDMiyoshiBankTodayViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //TableView配置
    [self ConfigTableView];
    
    
    
    if (self.XDMiyoshiBankTodayType == XDMiyoshiBankTodayJinRi)
    {
        [self ClickLeftButton:(UIButton *)[self.view viewWithTag:1100]];
    }
    
    if (self.XDMiyoshiBankTodayType == XDMiyoshiBankTodayJiLu)
    {
        //配置界面
        [self ConfigViewController];
    }
    
    
    /**  请求错题数与做题数 */
    [self RefreshProblemData];
    
}



#pragma mark - Config
/** TableView配置 */
- (void)ConfigTableView
{
    _RightTableView.tableHeaderView = [UIView new];
    _RightTableView.tableFooterView = [UIView new];
    
    [_RightTableView registerNib:[UINib nibWithNibName:[HNTodayCell description] bundle:nil] forCellReuseIdentifier:@"Cell"];
}


#pragma mark - 网络请求
/** 按钮网络请求中转 */
- (void)RefreshDataWithIndex:(NSInteger)Index
{
    //得到url与params
    [self GetUrlAndParamsWithIndex:Index Complete:^(NSString *UrlStr, NSDictionary *Dict) {
        
        [self RefreshDataWithUrlStr:UrlStr Dict:Dict];
    }];
}

/** 获取数据 */
- (void)RefreshDataWithUrlStr:(NSString *)UrlStr Dict:(NSDictionary *)Dict
{
    
    if (self.XDMiyoshiBankTodayType == 0)
    {
        self.Model = nil;
        [_RightTableView reloadData];
        [MBProgressHUD showHUD];
    }
    [XDHttpTool get:BaseURL(UrlStr) authCode:[AccountTool shareAccountTool].currentAccount.authCode params:Dict success:^(id responseObj) {
        
        if (responseObj)
        {
            [self.Model SetDict:responseObj];
            dispatch_async(dispatch_get_main_queue(), ^{
                [_RightTableView reloadData];
                _FlowerLabel.text = [NSString stringWithFormat:@"今天的红花%.1f朵",[self.Model.total_flowers floatValue]];
                _StarLabel.text = [NSString stringWithFormat:@"今天的星星%.1f颗",[self.Model.total_star floatValue]];
                _HeartLabel.text = [NSString stringWithFormat:@"今天送出红心%.1f颗",[self.Model.send_star floatValue]];
            });
            
            dispatch_async(dispatch_get_main_queue(), ^{
                if (self.XDMiyoshiBankTodayType == XDMiyoshiBankTodayJiLu)
                {
                    [_RightTableView reloadData];
                }
            });
        }
        else
        {
            [MBProgressHUD showError:responseObj[@"data"][@"msg"]];
        }
    } failure:^(NSError *error) {
        
        [MBProgressHUD showError:XDNetWorkError];
    }];
}


//结束刷新
- (void)JiLU_EndRefresh
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [_RightTableView.mj_footer endRefreshing];
        [_RightTableView.mj_header endRefreshing];
    });
}



/** 获取做题数与错题数。。。。嗯，要自己计算数量 */
- (void)RefreshProblemData
{
    NSDictionary *Dict = @{@"uid":[AccountTool shareAccountTool].currentAccount.uid};
    [XDHttpTool get:BaseURL(@"myresult.do?") authCode:[AccountTool shareAccountTool].currentAccount.authCode params:Dict success:^(id responseObj) {
        
        NSInteger Question_Count = 0;
        NSInteger Err_Count = 0;
        for (NSDictionary *Dict in responseObj)
        {
            Question_Count += [Dict[@"question_count"] integerValue];
            Err_Count += [Dict[@"error_count"] integerValue];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            _ErrProblemLabel.text = [NSString stringWithFormat:@"错题总数%ld道",Err_Count];
            _CountProblemLabel.text = [NSString stringWithFormat:@"做题总数%ld道",Question_Count];
            
        });
        
    } failure:^(NSError *error) {
        
    }];
}


#pragma mark - Function
/** 根据Index判断，提供数据url及params */
- (void)GetUrlAndParamsWithIndex:(NSInteger)Index Complete:(void(^)(NSString *,NSDictionary *))Complete
{
    //0好成绩 1好习惯 2好素养 3三好竞赛 4星星活动 5做题记录 6晚辅评价 7午托评语 8三好动态评价 9记录接口
    if (Index == 0)
    {
        Complete (@"user.do?action=getTodayMyFruit", @{@"uid":[AccountTool shareAccountTool].currentAccount.uid,@"role":@"4",@"type":@"1"});
    }
    else if (Index == 1)
    {
        Complete (@"user.do?action=getTodayMyFruit", @{@"uid":[AccountTool shareAccountTool].currentAccount.uid,@"role":@"4",@"type":@"5"});
    }
    else  if (Index == 2)
    {
        Complete (@"user.do?action=getTodayMyFruit", @{@"uid":[AccountTool shareAccountTool].currentAccount.uid,@"role":@"4",@"type":@"2"});
    }
    else if (Index == 3)
    {
        Complete (@"user.do?action=getTodayMyFruit", @{@"uid":[AccountTool shareAccountTool].currentAccount.uid,@"role":@"4",@"type":@"3"});
    }
    else if (Index == 4)
    {
        Complete (@"user.do?action=getTodayMyFruit", @{@"uid":[AccountTool shareAccountTool].currentAccount.uid,@"role":@"4",@"type":@"4"});
    }
    else if (Index == 5)
    {
        Complete (@"user.do?action=getTodayMyFruit", @{@"uid":[AccountTool shareAccountTool].currentAccount.uid,@"role":@"4",@"type":@"9"});
    }
    else if (Index == 6)
    {
        Complete (@"user.do?action=getTodayMyFruit", @{@"uid":[AccountTool shareAccountTool].currentAccount.uid,@"role":@"4",@"type":@"7"});
    }
    else if (Index == 7)
    {
        Complete (@"user.do?action=getTodayMyFruit", @{@"uid":[AccountTool shareAccountTool].currentAccount.uid,@"role":@"4",@"type":@"6"});
    }
    else if (Index == 8)
    {
        Complete (@"user.do?action=getTodayMyFruit", @{@"uid":[AccountTool shareAccountTool].currentAccount.uid,@"role":@"4",@"type":@"8"});
    }
    else
    {
        Complete (@"user.do?action=getMyFruitHistoryByDayTime",[self JiLu_Dict]);
    }
}



#pragma mark - TableViewViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.Model.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HNTodayCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    Cell.Model = self.Model.list[indexPath.row];
    return Cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
}



#pragma mark - Action
- (IBAction)ClickLeftButton:(UIButton *)sender
{
    _TempButton.selected = !_TempButton.selected;
    _TempButton = sender;
    _TempButton.selected = !_TempButton.selected;
    
    [self RefreshDataWithIndex:sender.tag-1100];
}


- (HNTodayModel *)Model
{
    if (!_Model)
    {
        self.Model = [[HNTodayModel alloc] init];
    }
    return _Model;
}

#pragma mark - ************** Type为JiLu时的方法在下面   **************
- (void)ConfigViewController
{
    self.title = @"记录";
    
    for (UIButton *Button in _Buttons)
    {
        Button.userInteractionEnabled = NO;
    }
    [self RefreshDataWithIndex:9];
}
- (NSDictionary *)JiLu_Dict
{
    return @{
             @"uid"       :[AccountTool shareAccountTool].currentAccount.uid,
             @"dateTime"  :self.DateTime,
             @"role"      :@4,
             @"page"      :@1
             };
}


@end
